package com.toman.ui.work.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Adpter.StateUserExpandableListViewAdapter;
import com.toman.Entity.StateSelect_Entity;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.MenuBean;
import com.toman.JavaBean.SysRoleEntity;
import com.toman.R;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.toolbar.ToolBarActivity;
import com.toman.widget.CustomerExpandableListView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class RoleInfActivity extends ToolBarActivity {
    String id;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_company_id)
    TextView tvCompanyId;
    @BindView(R.id.tv_title2)
    TextView tvTitle2;
    @BindView(R.id.tv_title3)
    TextView tvTitle3;
    @BindView(R.id.tv_title4)
    TextView tvTitle4;
    @BindView(R.id.cv_base)
    CardView cvBase;

    @BindView(R.id.cv_more)
    CardView cvMore;


    @BindView(R.id.ll)
    LinearLayout ll;

    private ArrayList<String> arrayList = new ArrayList<String>();
    private ArrayList<String> arrayList2 = new ArrayList<String>();

    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private ArrayList<ArrayList<StateSelect_Entity>> array = new ArrayList<>();
    private ArrayList<ArrayList<StateSelect_Entity>> array2 = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_role_inf);
        ButterKnife.bind(this);
        initToolBar("角色详情");
        initView();
        get();
        role();
    }

    private void initView() {
        id = getIntent().getStringExtra("id");

    }

    public void get() {
        OkHttpUtils
                .get(NewBaseUrl.role_info_get + id)
                .params("token", token)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "角色详情: " + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());
                        if (code == 0) {
                            SysRoleEntity bean = (SysRoleEntity) JSON.parseObject(jsonObj.getString("role"), SysRoleEntity.class);

                            tvTitle.setText(bean.getRoleName());
                            tvTitle2.setText(bean.getRoleNum());
                            tvTitle3.setText(bean.getCreateTime().toString());
                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }

    public void role() {
        OkHttpUtils
                .get(NewBaseUrl.role_info_app)
                .params("token", token)
                .params("roleName","")
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "角色权限: " + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());
                        if (code == 0) {
//                            arrayList.clear();
//                            array.clear();
//                            arrayList2.clear();
//                            array2.clear();
//                            MenuBean bean = (MenuBean) JSON.parseObject(result, MenuBean.class);
//                            for (int i = 0; i < bean.getSysEmployeeMenus().get(0).getList().size(); i++) {
//                                arrayList.add(bean.getSysEmployeeMenus().get(0).getList().get(i).getName());
//                                ArrayList<StateSelect_Entity> arrayList1 = new ArrayList<StateSelect_Entity>();
//                                if (bean.getSysEmployeeMenus().size() <= 0) {
//                                    return;
//                                }
//                                for (int k = 0; k < bean.getSysEmployeeMenus().get(0).getList().get(i).getList().size(); k++) {
//                                    StateSelect_Entity en1;
//
//                                    en1 = new StateSelect_Entity(1, bean.getSysEmployeeMenus().get(0).getList().get(i).getList().get(k).getName(), "", true);
//                                    arrayList1.add(en1);
//                                }
//                                array.add(arrayList1);
//
//
//                            }
//                            if (array.size() <= 0) {
//                                tvMenu.setVisibility(View.GONE);
//                            }
//                            if (bean.getSysEmployeeMenus().size() <= 1) {
//                                return;
//                            }
//                            for (int i = 0; i < bean.getSysEmployeeMenus().get(1).getList().size(); i++) {
//                                arrayList2.add(bean.getSysEmployeeMenus().get(1).getList().get(i).getName());
//                                ArrayList<StateSelect_Entity> arrayList1 = new ArrayList<StateSelect_Entity>();
//
//                                for (int k = 0; k < bean.getSysEmployeeMenus().get(1).getList().get(i).getList().size(); k++) {
//                                    StateSelect_Entity en1;
//
//                                    en1 = new StateSelect_Entity(1, bean.getSysEmployeeMenus().get(1).getList().get(i).getList().get(k).getName(), "", true);
//                                    arrayList1.add(en1);
//                                }
//
//                                array2.add(arrayList1);
//
//                            }
//                            if (array2.size() <= 0) {
//                                tvDevice.setVisibility(View.GONE);
//                            }
//
//                            stateAdpter.notifyDataSetChanged();
//                            stateAdpter2.notifyDataSetChanged();

                            MenuBean bean = (MenuBean) JSON.parseObject(result, MenuBean.class);
                            for (int j = 0; j < bean.getSysEmployeeMenus().size(); j++) {
                                ArrayList<String> arrayList = new ArrayList<String>();
                                ArrayList<ArrayList<StateSelect_Entity>> array = new ArrayList<>();
                                StateUserExpandableListViewAdapter stateAdpter = new StateUserExpandableListViewAdapter(getApplicationContext(), arrayList, array, myApplication);
                                CustomerExpandableListView mStateExpandable = new CustomerExpandableListView(getApplicationContext());
                                mStateExpandable.setAdapter(stateAdpter);
                                TextView tv1 = new TextView(getApplicationContext());
                                for (int i = 0; i < bean.getSysEmployeeMenus().get(j).getList().size(); i++) {


                                    arrayList.add(bean.getSysEmployeeMenus().get(j).getList().get(i).getName());

                                    ArrayList<StateSelect_Entity> arrayList1 = new ArrayList<StateSelect_Entity>();
                                    if (bean.getSysEmployeeMenus().size() <= 0) {
                                        continue;
                                    }
                                    try {
                                        for (int k = 0; k < bean.getSysEmployeeMenus().get(j).getList().get(i).getList().size(); k++) {
                                            StateSelect_Entity en1;

                                            en1 = new StateSelect_Entity(1, bean.getSysEmployeeMenus().get(j).getList().get(i).getList().get(k).getName(), "", true);
                                            arrayList1.add(en1);
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    array.add(arrayList1);
                                }
                                tv1.setTextSize(16);
                                tv1.setText(bean.getSysEmployeeMenus().get(j).getName() + "权限");
                                tv1.setTextColor(getResources().getColor(R.color.button_blue));
                                tv1.setPadding(10, 10, 10, 10);
                                mStateExpandable.setGroupIndicator(null);

                                ll.addView(tv1);
                                ll.addView(mStateExpandable);
                                stateAdpter.notifyDataSetChanged();
                            }
                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }
}
